package com.fulihui.wgj.dal.mybatis.repository;

import com.fulihui.wgj.dal.mybatis.domain.WxQrcodeEventStat;
import com.fulihui.wgj.dal.mybatis.mapper.WxQrcodeEventStatMapper;
import com.fulihui.wgj.facade.request.qrcode.WxQrcodePageQueryRequest;
import com.fulihui.wgj.facade.request.qrcode.WxQrcodeQueryRequest;
import com.fulihui.wgj.facade.request.qrcode.WxQrcodeStatDetailPageQueryRequest;
import com.fulihui.wgj.facade.request.qrcode.WxQrcodeStatDetailQueryRequest;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author wang_wx
 * @date 2018-06-20
 */
@Component
public class WxQrcodeEventStatRepository extends BaseRepository<WxQrcodeEventStat> {

    @Resource
    WxQrcodeEventStatMapper wxQrcodeEventStatMapper;

    /**
     * 查询二维码统计信息
     *
     * @param sceneIds
     * @param qrcodeQueryRequest
     * @return
     */
    public List<WxQrcodeEventStat> sumEventStat(List<String> sceneIds, WxQrcodePageQueryRequest qrcodeQueryRequest) {
        return wxQrcodeEventStatMapper.pageSumEventStat(sceneIds, qrcodeQueryRequest);
    }

    /**
     * 查询二维码统计信息
     *
     * @param sceneIds
     * @param qrcodeQueryRequest
     * @return
     */
    public List<WxQrcodeEventStat> sumEventStat(List<String> sceneIds, WxQrcodeQueryRequest qrcodeQueryRequest) {
        return wxQrcodeEventStatMapper.sumEventStat(sceneIds, qrcodeQueryRequest);
    }

    /**
     * 查询二维码统计信息-详细
     *
     * @param request
     * @return
     */
    public List<WxQrcodeEventStat> queryDetailStat(WxQrcodeStatDetailQueryRequest request) {
        List<WxQrcodeEventStat> wxQrcodeEventStats = wxQrcodeEventStatMapper.selectByExample(buildExample(request));
        return wxQrcodeEventStats;
    }

    /**
     * 查询二维码统计信息-详细-分页
     *
     * @param request
     * @return
     */
    public PageInfo<WxQrcodeEventStat> pageQueryDetailStat(WxQrcodeStatDetailPageQueryRequest request) {
        PageHelper.startPage(request.getPage(), request.getRows());
        List<WxQrcodeEventStat> wxQrcodeEventStats = wxQrcodeEventStatMapper.selectByExample(buildExample(request));
        return new PageInfo<>(wxQrcodeEventStats);
    }

    Example buildExample(WxQrcodeStatDetailQueryRequest request) {
        Example example = new Example(WxQrcodeEventStat.class);
        Example.Criteria criteria = example.createCriteria();
        if (StringUtils.isNotBlank(request.getAppId())) {
            criteria.andEqualTo("appId", request.getAppId());
        }
        if (StringUtils.isNotBlank(request.getSceneId())) {
            criteria.andEqualTo("sceneId", request.getSceneId());
        }
        if (StringUtils.isNotBlank(request.getStartDate())) {
            criteria.andGreaterThanOrEqualTo("gmtAttention", request.getStartDate());
        }
        if (StringUtils.isNotBlank(request.getEndDate())) {
            criteria.andLessThanOrEqualTo("gmtAttention", request.getEndDate());
        }
        example.orderBy("gmtAttention").desc();
        return example;
    }

    Example buildExample(WxQrcodeStatDetailPageQueryRequest request) {
        Example example = new Example(WxQrcodeEventStat.class);
        Example.Criteria criteria = example.createCriteria();
        if (StringUtils.isNotBlank(request.getAppId())) {
            criteria.andEqualTo("appId", request.getAppId());
        }
        if (StringUtils.isNotBlank(request.getSceneId())) {
            criteria.andEqualTo("sceneId", request.getSceneId());
        }
        if (StringUtils.isNotBlank(request.getStartDate())) {
            criteria.andGreaterThanOrEqualTo("gmtAttention", request.getStartDate());
        }
        if (StringUtils.isNotBlank(request.getEndDate())) {
            criteria.andLessThanOrEqualTo("gmtAttention", request.getEndDate());
        }
        example.orderBy("gmtAttention").desc();
        return example;
    }

    /**
     * 保存二维码统计信息-根据SQL
     *
     * @param eventStat
     * @return
     */
    public int saveStatBySql(WxQrcodeEventStat eventStat) {
        return wxQrcodeEventStatMapper.saveStatBySql(eventStat);
    }
}
